package com.feng.cloud.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.cloud.common.exception.system.ParameterException;
import com.feng.cloud.common.exception.system.SystemStatus;
import com.feng.cloud.common.request.QueryRequest;
import com.feng.cloud.common.response.TableResultResponse;
import com.feng.cloud.common.utils.SortUtils;
import com.feng.cloud.system.entity.GeneratorConfig;
import com.feng.cloud.system.mapper.GeneratorConfigMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName GeneratorGeneratorConfigService
 * @Description TODO
 * @Author Better Feng
 * @Date 2019/12/23 15:03
 * @Version 1.0
 **/
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
@AllArgsConstructor
public class GeneratorConfigService extends ServiceImpl<GeneratorConfigMapper, GeneratorConfig> {

    @Transactional
    public void updateGeneratorConfig(GeneratorConfig generatorConfig) {
        this.saveOrUpdate(generatorConfig);
    }

    /**
     * 列表查询
     * @param request QueryRequest
     * @return TableResultResponse<GeneratorConfig>
     */
    public TableResultResponse<GeneratorConfig> selectPage(QueryRequest request) {
        Page<GeneratorConfig> page = new Page<>();
        SortUtils.handlePageSort(request,page);
        LambdaQueryWrapper<GeneratorConfig> queryWrapper = new LambdaQueryWrapper<>();
        String serviceName = request.getValue(GeneratorConfig::getServiceName, String.class);
        if (StringUtils.isNotBlank(serviceName)) {
            queryWrapper.like(GeneratorConfig::getServiceName, serviceName);
        }
        IPage<GeneratorConfig> iPage = this.baseMapper.selectPage(page, queryWrapper);
        return new TableResultResponse<>(iPage.getTotal(), iPage.getRecords());
    }

    public GeneratorConfig findConfigByServiceName(String serviceName) {
        LambdaQueryWrapper<GeneratorConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GeneratorConfig::getServiceName, serviceName);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    public GeneratorConfig selectGenConfigByServiceName(String serviceName) {
        LambdaQueryWrapper<GeneratorConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GeneratorConfig::getServiceName, serviceName);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    public List<GeneratorConfig> selectServiceList(String tableSchema) {
        LambdaQueryWrapper<GeneratorConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isBlank(tableSchema)) {
            throw new ParameterException(SystemStatus.TABLE_SCHEMA_EMPTY);
        }
        queryWrapper.eq(GeneratorConfig::getTableSchema, tableSchema);
        return this.baseMapper.selectList(queryWrapper);
    }
}
